from core import *
import matplotlib.pyplot as plt
import numpy as np
from visualizer.basic import *
from sklearn.metrics import mean_squared_error


def Ts(ts2, ts1, l, m, step, func):
	return TE(ts2, ts1, l, m, step, func) - TE(ts1, ts2, l, m, step, func)


def binning_4(subsequence):
	b = 4
	if len(subsequence) <= b:
		return array2index(np.argsort(subsequence))
	_max = max(subsequence)
	_min = min(subsequence)
	_range = (_max - _min)/b
	ans = np.digitize(subsequence, bins=np.arange(min(subsequence), max(subsequence)+0.1, _range))
	return array2index(ans)


def binning_5(subsequence):
	b = 5
	if len(subsequence) <= b:
		return array2index(np.argsort(subsequence))
	_max = max(subsequence)
	_min = min(subsequence)
	_range = (_max - _min)/b
	ans = np.digitize(subsequence, bins=np.arange(min(subsequence), max(subsequence)+0.1, _range))
	return array2index(ans)


def binning_6(subsequence):
	b = 6
	if len(subsequence) <= b:
		return array2index(np.argsort(subsequence))
	_max = max(subsequence)
	_min = min(subsequence)
	_range = (_max - _min)/b
	ans = np.digitize(subsequence, bins=np.arange(min(subsequence), max(subsequence)+0.1, _range))
	return array2index(ans)


if __name__ == '__main__':
	print('ing...')
	ts1 = np.loadtxt('../data/pr_NY.csv')  # 降水量 # Y
	ts2 = np.loadtxt('../data/tas_NY.csv')  # 温度 # X
	N = 21
	l = 1
	m = 3
	step = 1
	# symbol
	symbol_0 = []
	x = list(range(3, N))
	for m in range(3, N):
		T = Ts(ts2, ts1, l, m, step, symbol)
		symbol_0.append(T)
	plt.plot(x, symbol_0, label='STE')
	# --------------------------------------------------------------------------------
	# par
	par1 = []
	for m in range(3, N):
		T = Ts(ts2, ts1, l, m, step, binning_4)
		par1.append(T)
	plt.plot(x, par1, label='$b=4$')
	# par
	par2 = []
	for m in range(3, N):
		T = Ts(ts2, ts1, l, m, step, binning_5)
		par2.append(T)
	plt.plot(x, par2, label='$b=5$')
	# par
	par3 = []
	for m in range(3, N):
		T = Ts(ts2, ts1, l, m, step, binning_6)
		par3.append(T)
	plt.plot(x, par3, label='$b=6$')
	# --------------------------------------------------------------------------------
	plt.xlabel('$m$')
	plt.ylabel('$T(m)$')
	plt.xticks(np.arange(3, N, 3), np.arange(3, N, 3))
	plt.legend()
	plt.tight_layout()
	plt.savefig(PATH + 'BinningTE.pdf', format='pdf')
	plt.show()
	mse1 = mean_squared_error(symbol_0, par1)
	mse2 = mean_squared_error(symbol_0, par2)
	mse3 = mean_squared_error(symbol_0, par3)
	print('MSE($b$=4):', mse1)
	print('MSE($b$=5):', mse2)
	print('MSE($b$=6):', mse3)
